package interrupt;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: hmm
 * @CreateDate: 2019/8/21
 * @UpdateUser:
 * @UpdateDate: 2019/8/21
 * @UpdateRemark: 修改备注
 * @Version: 1.0
 */
public class InterruptDemo {
    /**
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     *        核心线程数，即时线程池中有空闲线程，线程数小于此值的话，仍然会创建新的线程
     *
     * @param maximumPoolSize the maximum number of threads to allow in the
     *        pool
     *        最大线程数，线程池所能容的最多线程数量
     *
     * @param keepAliveTime when the number of threads is greater than
     *        the core, this is the maximum time that excess idle threads
     *        will wait for new tasks before terminating.
     *        保活时间，当线程数量大于核心线程数，此时的空闲线程等待新任务的最长时间
     *
     * @param unit the time unit for the {@code keepAliveTime} argument
     *        保活时间的计量单位
     *
     * @param workQueue the queue to use for holding tasks before they are
     *        executed.  This queue will hold only the {@code Runnable}
     *        tasks submitted by the {@code execute} method.
     *        工作队列，任务执行前用来持有任务的，仅持有excute方法提交的Runnable任务
     *
     * @param threadFactory the factory to use when the executor
     *        creates a new thread
     *        线程工厂，在执行器创建线程时使用
     *
     * @param handler the handler to use when execution is blocked
     *        because the thread bounds and queue capacities are reached
     *        处理器，由于线程bounds或者队列容量已满，导致线程阻塞时执行的逻辑
     *
     * @throws IllegalArgumentException if one of the following holds:<br>
     *         {@code corePoolSize < 0}<br>
     *         {@code keepAliveTime < 0}<br>
     *         {@code maximumPoolSize <= 0}<br>
     *         {@code maximumPoolSize < corePoolSize}
     *         如果出现下列之一抛出的异常
     *
     * @throws NullPointerException if {@code workQueue}
     *         or {@code threadFactory} or {@code handler} is null
     *         如果三者有一个为null抛出的异常
     */
    private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,10,30, TimeUnit.SECONDS,
            new LinkedBlockingDeque<Runnable>(), Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }
        threadPoolExecutor.shutdown();
    }
}
